package org.cqut.system.notice.controller;

import org.cqut.framework.web.response.ResultInfo;
import org.cqut.framework.web.response.Status;
import org.cqut.system.notice.domain.Notice;
import org.cqut.system.notice.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/**
 * @author XiAoXu
 * @date 2021/11/14 23:20
 * @description
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @GetMapping(path = "/list/{courseId}")
    public ResultInfo getNoticeList(@PathVariable String courseId) {
        List<Notice> notices = noticeService.getNoticeList(courseId);
        notices.sort(Comparator.comparingInt(Notice::getStatus));
        if (!notices.isEmpty()) {
            return new ResultInfo(Status.QUERY_DETAILS_SUCCESS.code, Status.QUERY_DETAILS_SUCCESS.message, notices);
        } else {
            return new ResultInfo(Status.QUERY_DETAILS_ERROR.code, Status.QUERY_DETAILS_ERROR.message);
        }
    }

    @GetMapping(path = "/listForStudent/{courseId}")
    public ResultInfo getNoticeListForStudent(@PathVariable String courseId) {
        List<Notice> notices = noticeService.getNoticeListForStudent(courseId);
        if (!notices.isEmpty()) {
            return new ResultInfo(Status.QUERY_DETAILS_SUCCESS.code, Status.QUERY_DETAILS_SUCCESS.message, notices);
        } else {
            return new ResultInfo(Status.QUERY_DETAILS_ERROR.code, Status.QUERY_DETAILS_ERROR.message);
        }
    }

    @GetMapping(path = "/{courseId}")
    public ResultInfo getNotice(@PathVariable String courseId) {
        Notice notice = noticeService.getById(courseId);
        if (Objects.nonNull(notice)) {
            return new ResultInfo(Status.QUERY_DETAILS_SUCCESS.code, Status.QUERY_DETAILS_SUCCESS.message, notice);
        } else {
            return new ResultInfo(Status.QUERY_DETAILS_ERROR.code, Status.QUERY_DETAILS_ERROR.message);
        }
    }

    @DeleteMapping("/{noticeId}")
    public ResultInfo removeNotice(@PathVariable String noticeId) {
        boolean flag = noticeService.removeById(noticeId);
        if (flag) {
            return new ResultInfo(Status.SUCCESS.code, Status.SUCCESS.message);
        } else {
            return new ResultInfo(Status.WARN.code, Status.WARN.message);
        }
    }

    @PostMapping("/edit")
    public ResultInfo editNotice(@RequestBody Notice notice) {
        boolean flag = noticeService.updateById(notice);
        if (flag) {
            return new ResultInfo(Status.SUCCESS.code, Status.SUCCESS.message);
        } else {
            return new ResultInfo(Status.WARN.code, Status.WARN.message);
        }
    }

    @PostMapping()
    public ResultInfo createNotice(@RequestBody Notice notice) {
        boolean flag = noticeService.save(notice);
        if (flag) {
            return new ResultInfo(Status.SUCCESS.code, Status.SUCCESS.message);
        } else {
            return new ResultInfo(Status.WARN.code, Status.WARN.message);
        }
    }

    @PostMapping("/publish/{noticeId}")
    public ResultInfo publishNotice(@PathVariable String noticeId) {
        boolean flag = noticeService.publishNotice(noticeId);
        if (flag) {
            return new ResultInfo(Status.SUCCESS.code, Status.SUCCESS.message);
        } else {
            return new ResultInfo(Status.WARN.code, Status.WARN.message);
        }
    }
}
